import os
import sys


""" 
cachebuilder.py by Peter Balogh palexanderbalogh@yahoo.com
to be used in conjunction with CacheBuilder.as

part of a utility to 
a) load a large number of files into the browser cache, so that you can pre-load files
for instant access later, and 

b) provide real, accurate feedback during this preloading process, using knowledge of file sizes and containing folders

this script gets invoked -- either by ant or at the command line --
 with a series of arguments that tell it which folders to scan for files.
 and it will scan those folders recursively,
 which means it will get nested folders -- you don't need to specify each one.
 
 """

totalsize = 0 # the total size of all files we encounter 
xmlstring = ""

def walkfun ( arg, current_directory, file_list):
	global folderxmlstring
	global foldersize
	if current_directory.find( ".svn" ) == -1:
		for fi in file_list:
			f = os.path.join ( current_directory, fi )
			if not os.path.isdir( f ):		
				print "for file " + f  + ", in " + arg + ", size is ",
				print os.path.getsize( f  )
				folderxmlstring += "<file name='" + f + "' size='" + str( os.path.getsize( f ) ) + "' />"
				foldersize += os.path.getsize( f )
		global totalsize
		totalsize += foldersize
		
		
totalxmlstring = ""


# don't start with 0, since the first arg would always be the name of this script....
for foldername in sys.argv[ 1: ]:
	foldersize = 0
	folderxmlstring = ""
	os.path.walk( foldername, walkfun, foldername ) # recursively dig into each folder
	folderxmlstring = "<folder name='" + foldername + "' size='" + str( foldersize ) + "' >" + folderxmlstring + "</folder>"
	print "folderxmlstring=", folderxmlstring
	totalxmlstring += folderxmlstring
	
totalxmlstring = "<files size='" + str( totalsize ) + "' >" + totalxmlstring + "</files>"
print "totalsize = ", totalsize
print "totalxmlstring = ", totalxmlstring
f = open( "preloadcache.xml", "w" )
f.write( totalxmlstring )
f.close()

